package com.zxl.manager.privacy.utils.utils;

import android.text.TextUtils;

import java.lang.reflect.Field;

/**
 * 类工具
 * @author makai
 *
 */
public class ClassUtils {
	
	/**
	 * 获取类的属性
	 * @param cls			getClass()
	 * @param object		this
	 * @param escape		转移字符
	 * @return
	 */
	public static String toString(Class<?> cls, Object object, String escape) {
		StringBuffer buffer = new StringBuffer();
		if (null != cls) {
			String classFields = getClassFields(cls, object, escape);
			if (!TextUtils.isEmpty(classFields)) {
				buffer.append(classFields);
			}
			String classFields2 = getClassFields(cls.getSuperclass(), object, escape);
			if (!TextUtils.isEmpty(classFields2)) {
				buffer.append(classFields2);
			}
		}
		return buffer.toString();
	}
	
	/**
	 * 获取类的属性
	 * @param cls
	 * @param object
	 * @param escape
	 * @return
	 */
	private static String getClassFields(Class<?> cls, Object object, String escape) {
		StringBuffer buffer = new StringBuffer();
		if (null != cls) {
			Field[] fields = cls.getDeclaredFields();
			if (null != fields) {
				for (int i = 0; i < fields.length; i++) {
					Field field = fields[i];
					field.setAccessible(true);
					try {
						Object object1 = field.get(object);
						if (null != object) {
							if (!TextUtils.isEmpty(escape)) {
								buffer.append(escape);
							}
							buffer.append(field.getName()).append(":").append(object1);
							LogUtils.d(field.getName() + " = " + object1);
						}
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					}
				}
			}
		}
		return buffer.toString();
	}
	
}
